home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / ispell40.lha / ispell-4.0 / lexfsm.c < prev    next >
C/C++ Source or Header  |  1993-05-31  |  4KB  |  212 lines

  1. /* Copyright (C) 1990, 1993 Free Software Foundation, Inc.
  2.  
  3.    This file is part of GNU ISPELL.
  4.  
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 2, or (at your option)
  8.    any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program; if not, write to the Free Software
  17.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include <stdio.h>
  20.  
  21. #ifdef HAVE_MALLOC_H
  22. #include <malloc.h>
  23. #endif
  24.  
  25. #include "ispell.h"
  26.  
  27. unsigned char **lextable;
  28. unsigned char *lexchar;
  29. char **lexdecode;
  30. int lexletters;
  31.  
  32. /* if out is an odd number of bytes long, must zero the
  33.  * remaining byte because hash() works 16 bits at a time.
  34.  * therefore, always write 2 zeros
  35.  *
  36.  * return the number of bytes in the compessed output.
  37.  *
  38.  * If the string contains a character that is not in the lexchar[]
  39.  * array, then the word cannot possiblally be in the dictionary.
  40.  * Therefore return 0.
  41.  */
  42. int
  43. lexword (in, len, out)
  44.   char *in;
  45.   int len;
  46.   unsigned char *out;
  47. {
  48.   unsigned char c1, c2, code;
  49.   unsigned char *startout;
  50.   startout = out;
  51. newc1:
  52.   if (len-- == 0)
  53.     {
  54.       out[0] = 0;
  55.       out[1] = 0;
  56.       return (out - startout);
  57.     }
  58.  
  59.   c1 = lexchar[*(unsigned char *) in++];
  60.   if (c1 == 0)
  61.     {
  62.       /* this character was not used anywhere in the dictionary
  63.          * return 0 to tell lookup() not to bother looking in
  64.          * the hash table
  65.          */
  66.       return (0);
  67.     }
  68.  
  69.   if (len == 0)
  70.     {
  71.       *out++ = c1;
  72.       out[0] = 0;
  73.       out[1] = 0;
  74.       return (out - startout);
  75.     }
  76.  
  77.   while (len--)
  78.     {
  79.       /* c1 is a good letter */
  80.       c2 = lexchar[*(unsigned char *) in++];
  81.       code = lextable[c1][c2];
  82.       switch (code)
  83.     {
  84.     case 255:        /* c2 is not a letter */
  85.       return (0);
  86.     case 0:        /* both letters, but no code for this pair */
  87.       *out++ = c1;
  88.       c1 = c2;
  89.       break;
  90.     default:
  91.       *out++ = code;
  92.       goto newc1;
  93.     }
  94.     }
  95.   *out++ = c1;
  96.   out[0] = 0;
  97.   out[1] = 0;
  98.   return (out - startout);
  99. }
  100.  
  101.  
  102. void
  103. lexprint (out, verbose)
  104.   unsigned char *out;
  105.   int verbose;
  106. {
  107.   char *p;
  108.   char *left, *right;
  109.  
  110.   if (verbose)
  111.     {
  112.       left = "<";
  113.       right = ">";
  114.     }
  115.   else
  116.     {
  117.       left = "";
  118.       right = "";
  119.     }
  120.   while (*out)
  121.     {
  122.       p = lexdecode[*out];
  123.       if (*p == 0)
  124.     (void) printf ("{illegal code %d}", *out);
  125.       else if (p[1] == 0)
  126.     putchar (p[0]);
  127.       else
  128.     printf ("%s%s%s", left, p, right);
  129.       out++;
  130.     }
  131. }
  132.  
  133. int
  134. lexsize (NOARGS)
  135. {
  136.   return (256 * 2 + 256 + lexletters * lexletters);
  137. }
  138.  
  139.  
  140. void
  141. lexdump (f)
  142.   FILE *f;
  143. {
  144.   int i, j;
  145.  
  146.   for (i = 0; i < 256; i++)
  147.     {
  148.       (void) putc (lexdecode[i][0], f);
  149.       (void) putc (lexdecode[i][1], f);
  150.     }
  151.   for (i = 0; i < 256; i++)
  152.     (void) putc ((int) lexchar[i], f);
  153.  
  154.   for (i = 0; i < lexletters; i++)
  155.     for (j = 0; j < lexletters; j++)
  156.       (void) putc ((int) lextable[i][j], f);
  157. }
  158.  
  159.  
  160. void
  161. lexalloc (NOARGS)
  162. {
  163.   int i;
  164.   char *p;
  165.  
  166.  
  167.   lexdecode = (char **) xmalloc (256 * sizeof (char *));
  168.   p = (char *) xcalloc (256, 3);
  169.  
  170.   for (i = 0; i < 256; i++)
  171.     {
  172.       lexdecode[i] = p;
  173.       p += 3;
  174.     }
  175.  
  176.   lexchar = (unsigned char *) xcalloc (256, 1);
  177.   lextable = (unsigned char **) xmalloc (lexletters * sizeof (char *));
  178.  
  179.   for (i = 0; i < lexletters; i++)
  180.     lextable[i] = (unsigned char *) xcalloc ((unsigned) lexletters, 1);
  181. }
  182.  
  183.  
  184. void
  185. lexload (f, nlet)
  186.   FILE *f;
  187.   unsigned long nlet;
  188. {
  189.   int i, j;
  190.   unsigned char *up;
  191.   char *p;
  192.  
  193.   lexletters = (int) nlet;
  194.   lexalloc ();
  195.   for (i = 0; i < 256; i++)
  196.     {
  197.       p = lexdecode[i];
  198.       *p++ = getc (f);
  199.       *p++ = getc (f);
  200.       *p = 0;
  201.     }
  202.  
  203.   for (i = 0, up = lexchar; i < 256; i++, up++)
  204.     *up = getc (f);
  205.  
  206.   for (i = 0; i < lexletters; i++)
  207.     {
  208.       for (j = 0; j < lexletters; j++)
  209.     lextable[i][j] = getc (f);
  210.     }
  211. }
  212.